package com.xyht.medicine_management_system.modules.kuaidi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.medicine_management_system.common.commonReturn.CommonCodeEnum;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.modules.kuaidi.entity.MmsKuaidiCompany;
import com.xyht.medicine_management_system.modules.kuaidi.entity.MmsKuaidiInfo;
import com.xyht.medicine_management_system.modules.kuaidi.entity.req.MmsKuaidiCompanyReq;
import com.xyht.medicine_management_system.modules.kuaidi.entity.resp.MmsKuaidiCompanyResp;
import com.xyht.medicine_management_system.modules.kuaidi.mapper.MmsKuaidiCompanyMapper;
import com.xyht.medicine_management_system.modules.kuaidi.service.MmsKuaidiCompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.medicine_management_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 快递公司 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-07-03
 */
@Service
public class MmsKuaidiCompanyServiceImpl extends ServiceImpl<MmsKuaidiCompanyMapper, MmsKuaidiCompany> implements MmsKuaidiCompanyService {
    @Resource
    private MmsKuaidiCompanyMapper mmsKuaidiCompanyMapper;

    @Override
    public ResponseResult addKuaidiCompany(MmsKuaidiCompanyReq companyReq) {
        if (isNullOrEmpty(companyReq.getComCode(), companyReq.getComName())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        List<MmsKuaidiCompany> list = mmsKuaidiCompanyMapper.selectList(new LambdaQueryWrapper<MmsKuaidiCompany>()
                .eq(MmsKuaidiCompany::getComCode, companyReq.getComCode()));
        if (list != null && list.size() > 0) {
            return CommonResult.failed(CommonCodeEnum.KUAIDI_COMPANY_EXIST);
        }

        MmsKuaidiCompany addCompany = new MmsKuaidiCompany();
        addCompany.setComCode(companyReq.getComCode());
        addCompany.setComName(companyReq.getComName());
        mmsKuaidiCompanyMapper.insert(addCompany);
        return CommonResult.success();
    }

    @Override
    public ResponseResult updateKuaidiCompany(MmsKuaidiCompanyReq companyReq) {
        if (isNullOrEmpty(companyReq.getComCode(), companyReq.getComName(),
                companyReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsKuaidiCompany company = mmsKuaidiCompanyMapper.selectById(companyReq.getId());
        if (company == null) {
            return CommonResult.failed(CommonCodeEnum.KUAIDI_COMPANY_NOT_EXIST);
        }

        if (!company.getComCode().equals(companyReq.getComCode())) {
            List<MmsKuaidiCompany> list = mmsKuaidiCompanyMapper.selectList(new LambdaQueryWrapper<MmsKuaidiCompany>()
                    .eq(MmsKuaidiCompany::getComCode, companyReq.getComCode()));
            if (list != null && list.size() > 0) {
                return CommonResult.failed(CommonCodeEnum.KUAIDI_COMPANY_EXIST);
            }
        }

        MmsKuaidiCompany updateCompany = new MmsKuaidiCompany();
        updateCompany.setId(companyReq.getId());
        updateCompany.setComCode(companyReq.getComCode());
        updateCompany.setComName(companyReq.getComName());
        mmsKuaidiCompanyMapper.updateById(updateCompany);
        return CommonResult.success();
    }

    @Override
    public ResponseResult deleteKuaidiCompany(MmsKuaidiCompanyReq companyReq) {
        if (isNullOrEmpty(companyReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        mmsKuaidiCompanyMapper.deleteById(companyReq.getId());

        return CommonResult.success();
    }

    @Override
    public ResponseResult getKuaidiCompanyList(String searchStr, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<MmsKuaidiCompany> queryWrapper = new LambdaQueryWrapper<>();
        if (!isNullOrEmpty(searchStr)) {
            queryWrapper.and(wrapper -> {
                wrapper.like(MmsKuaidiCompany::getComCode, searchStr)
                        .or()
                        .like(MmsKuaidiCompany::getComName, searchStr);
            });
        }
        queryWrapper.orderByAsc(MmsKuaidiCompany::getComCode);
        Page<MmsKuaidiCompany> page = new Page<>(pageNum, pageSize);
        mmsKuaidiCompanyMapper.selectPage(page, queryWrapper);

        List<MmsKuaidiCompanyResp> list = page.getRecords()
                .stream()
                .map(company -> {
                    MmsKuaidiCompanyResp resp = new MmsKuaidiCompanyResp();
                    BeanUtils.copyProperties(company, resp);
                    return resp;
                })
                .collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(list, total);
    }
}
